package io.gitee.sfch.chunmi.service.system.impl;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import io.gitee.sfch.chunmi.Tables;
import io.gitee.sfch.chunmi.common.enums.StatusEnum;
import io.gitee.sfch.chunmi.exception.DataAlreadyExistsException;
import io.gitee.sfch.chunmi.common.tool.PasswordTool;
import io.gitee.sfch.chunmi.controller.system.form.SystemUserAddForm;
import io.gitee.sfch.chunmi.controller.system.request.SystemUserPageRequest;
import io.gitee.sfch.chunmi.dao.system.entity.SystemUser;
import io.gitee.sfch.chunmi.dao.system.enums.SexEnum;
import io.gitee.sfch.chunmi.dao.system.mapper.SystemUserMapper;
import io.gitee.sfch.chunmi.entity.Pageable;
import io.gitee.sfch.chunmi.entity.Pagination;
import io.gitee.sfch.chunmi.service.system.SystemUserService;
import io.gitee.sfch.chunmi.service.system.converter.SystemUserConverter;
import io.gitee.sfch.chunmi.util.OptionalUtil;
import io.gitee.sfch.chunmi.util.PageUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class SystemUserServiceImpl implements SystemUserService {

    @Resource
    private SystemUserMapper systemUserMapper;
    @Resource
    private SystemUserConverter systemUserConverter;

    @Override
    public SystemUser findUser(String username) {
        return QueryChain.of(systemUserMapper)
                .where(Tables.systemUser.username.eq(username))
                .one();
    }

    @Override
    public Pagination<SystemUser> findPage(SystemUserPageRequest pageRequest, Pageable pageable) {
        Page<SystemUser> page = QueryChain.of(systemUserMapper)
                .where(Tables.systemUser.username.like(pageRequest.getUsername()))
                .where(Tables.systemUser.name.like(pageRequest.getName()))
                .where(Tables.systemUser.phoneNumber.like(pageRequest.getPhoneNumber()))
                .where(Tables.systemUser.status.eq(pageRequest.getStatus()))
                .where(Tables.systemUser.sex.eq(pageRequest.getSex()))
                .orderBy(Tables.systemUser.id.desc())
                .page(PageUtil.from(pageable));

        return PageUtil.from(page, pageable);
    }

    @Override
    public void add(SystemUserAddForm addForm) {
        SystemUser systemUser = systemUserConverter.of(addForm);
        if (this.hasUser(addForm.getUsername())) {
            throw new DataAlreadyExistsException(StrUtil.format("用户名已存在: {}", addForm.getUsername()));
        }
        // 密码加密存储
        String encryptedPassword = PasswordTool.encrypt(systemUser.getPassword());
        systemUser.setPassword(encryptedPassword);
        // 默认值，性别
        OptionalUtil.executeIfNull(systemUser.getSex(), () -> systemUser.setSex(SexEnum.unknown));
        // 状态
        systemUser.setStatus(StatusEnum.enabled);

        systemUserMapper.insert(systemUser);
    }

    @Override
    public boolean hasUser(String username) {
        return QueryChain.of(systemUserMapper)
                .where(Tables.systemUser.username.eq(username))
                .exists();
    }
}
